package cn.com.fattiger.framework.exchanger;

import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExchangerDemo {
	private static final Exchanger<String> exchanger = new Exchanger<String>();
	private static ExecutorService threadPool = Executors.newFixedThreadPool(2);
	
	public static void main(String[] args){
		threadPool.execute(new Runnable(){
			@Override 
			public void run(){
				String A = "A";
				try {
					exchanger.exchange(A);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		threadPool.execute(new Runnable(){
			@Override
			public void run(){
				try {
					String B = "B";
					String A = exchanger.exchange(B);
					System.out.println(A);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		threadPool.shutdown();
	}
}
